<style>
	tr.sub_row{background:#CCC;}
	tr.sub_row td{padding-left:20px;}
        .noLBorder td{border-left: 0px none; padding: 5px;}
        .nopad{padding:0px;text-align: center;}
        .textbox_quantity input{
           width:50px!important;
           padding:1px!important;
        }
        .box .textbox_quantity input.textbox_readonly:focus{
            border: none;
           -moz-box-shadow:none;
        }
        .textbox_readonly, .permanent_readonly{
            background: none;
            border: 0px;
        }
        .left{
            float: left;
}
.clear{
    clear:both;
}
</style>
<div class="componentheading"><?php echo $page_title;?></div>
<div id="show_msg"></div>
<!--<div style="padding-bottom:20px;">
	<form>
    	<input type="text"><input type="submit" value="search">
    </form>
        
    <a class="button" href="<?php echo site_url('quantity/add')?>">Add Quantity</a>
</div>
-->
<div class="box-header">Publication quantity list</div>
<div class="box table">
    <table width="100%">
        <thead>
          <tr>
                <th class="nopad" width="40%" height="32">Publication Name</th>
                <th class="nopad" width="10%" style="border-left:0px;"></th>
                <th class="nopad" width="10%">Quantity for distribution</th>
                <th class="nopad" width="10%">Quantity in box</th>
                <th class="nopad" width="10%">Quantity in store</th>
                <th class="nopad" width="10%">Grand total</th>
                <th class="nopad" width="10%">Action</th>
          </tr>
       </thead>
       <tbody>
          <?php if(count($quantities['en']) > 0): ?>
              <?php $cnt=0;foreach($quantities['en'] as $quantity) : 
                  ?>
       
                    <tr id="textbox_readonly-<?php echo $quantity->publication_id;?>" class="textbox_quantity">
                        <td class="nopad" colspan="7">
                            <form method="post" action="" id="frm_quantity_<?php echo $quantity->publication_id;?>">
                            <table width="100%" id="tbl_quantity_<?php echo $quantity->publication_id;?>">
                                <tr>
                        <td width="40%">
                            <div><strong><?php echo $quantity->publication_title." (".$quantity->publication_coding.")";?></strong></div>
                            <div>
                                <div class="left" style="width:25%"><img src="<?php echo config_item('publication_thumbs_path').$quantity->publication_image;?>" /></div>
                                <div class="left" style="width:75%"><?php echo word_limiter($quantity->publication_excerpt, 10);?></div>
                                <div class="clear"></div>
                            </div>
                        </td>
                        <td class="nopad" width="10%">
                              <table cellspacing="0" cellpadding="0" class="noLBorder">
                                <tr>
                                    <td>English:</td>
                                </tr>
                                <tr>
                                    <td>Nepali:</td>
                                </tr>
                                <tr>
                                    <td>Total:</td>
                                </tr>
                                </table>
                        </td>
                        <td class="nopad" width="10%">
                            <table cellspacing="0" cellpadding="0" class="noLBorder">
                                <tr>
                                    <td><input type="text" onkeydown="return suppressNonNumericInput(event)" readonly class="textbox_readonly calc_quantity" name="total_qty_distribution_en_<?php echo $quantity->publication_id;?>" id="total_qty_distribution_en_<?php echo $quantity->publication_id;?>" value="<?php echo $quantity->total_qty_distribution;?>"></td>
                                </tr>
                                <tr>
                                    <td><input type="text" onkeydown="return suppressNonNumericInput(event)" readonly class="textbox_readonly calc_quantity" name="total_qty_distribution_np_<?php echo $quantity->publication_id;?>" id="total_qty_distribution_np_<?php echo $quantity->publication_id;?>" value="<?php echo $quantities['np'][$cnt]->total_qty_distribution;?>"></td>
                                </tr>
                                <tr>
                                    <td><input type="text" readonly class="textbox_readonly permanent_readonly" name="total_qty_distribution_total_<?php echo $quantity->publication_id;?>" id="total_qty_distribution_total_<?php echo $quantity->publication_id;?>" value="<?php echo $quantities['np'][$cnt]->total_qty_distribution+$quantity->total_qty_distribution;?>"></td>
                                </tr>
                            </table>
                        </td>
                        <td class="nopad" width="10%">
                            <table cellspacing="0" cellpadding="0" class="noLBorder">
                                <tr>
                                   <td><input type="text" onkeydown="return suppressNonNumericInput(event)" readonly class="textbox_readonly calc_quantity" name="total_qty_box_en_<?php echo $quantity->publication_id;?>" id="total_qty_box_en_<?php echo $quantity->publication_id;?>" value="<?php echo $quantity->total_qty_box;?>"></td>
                                </tr>
                                <tr>
                                    <td><input type="text" onkeydown="return suppressNonNumericInput(event)" readonly class="textbox_readonly calc_quantity" name="total_qty_box_np_<?php echo $quantity->publication_id;?>" id="total_qty_box_np_<?php echo $quantity->publication_id;?>" value="<?php echo $quantities['np'][$cnt]->total_qty_box;?>"></td>
                                </tr>
                                <tr>
                                    <td><input type="text" readonly class="textbox_readonly permanent_readonly" name="total_qty_box_total_<?php echo $quantity->publication_id;?>" id="total_qty_box_total_<?php echo $quantity->publication_id;?>" value="<?php echo $quantities['np'][$cnt]->total_qty_box+$quantity->total_qty_box;?>"></td>
                                </tr>
                            </table>
                        </td>
                        <td class="nopad" width="10%">
                            <table cellspacing="0" cellpadding="0" class="noLBorder">
                                <tr>
                                   <td><input type="text" onkeydown="return suppressNonNumericInput(event)" readonly class="textbox_readonly calc_quantity" name="total_qty_store_en_<?php echo $quantity->publication_id;?>" id="total_qty_store_en_<?php echo $quantity->publication_id;?>" value="<?php echo $quantity->total_qty_store;?>"></td>
                                </tr>
                                <tr>
                                    <td><input type="text" onkeydown="return suppressNonNumericInput(event)" readonly class="textbox_readonly calc_quantity" name="total_qty_store_np_<?php echo $quantity->publication_id;?>" id="total_qty_store_np_<?php echo $quantity->publication_id;?>" value="<?php echo $quantities['np'][$cnt]->total_qty_store;?>"></td>
                                </tr>
                                <tr>
                                    <td><input type="text" readonly class="textbox_readonly permanent_readonly" name="total_qty_store_total_<?php echo $quantity->publication_id;?>" id="total_qty_store_total_<?php echo $quantity->publication_id;?>" value="<?php echo $quantities['np'][$cnt]->total_qty_store+$quantity->total_qty_store;?>"></td>
                                </tr>
                            </table>
                        </td>
                        <td class="nopad" width="10%">
                            <table cellspacing="0" cellpadding="0" class="noLBorder">
                                <tr>
                                   <td><input type="text" readonly class="textbox_readonly permanent_readonly" name="total_qty_grand_en_<?php echo $quantity->publication_id;?>" id="total_qty_grand_en_<?php echo $quantity->publication_id;?>" value="<?php echo $quantity->total_qty;?>"></td>
                                </tr>
                                <tr>
                                    <td><input type="text" readonly class="textbox_readonly permanent_readonly" name="total_qty_grand_np_<?php echo $quantity->publication_id;?>" id="total_qty_grand_np_<?php echo $quantity->publication_id;?>" value="<?php echo $quantities['np'][$cnt]->total_qty;?>"></td>
                                </tr>
                                <tr>
                                    <td><input type="text" readonly class="textbox_readonly permanent_readonly" name="total_qty_grand_total_<?php echo $quantity->publication_id;?>" id="total_qty_grand_total_<?php echo $quantity->publication_id;?>" value="<?php echo $quantities['np'][$cnt]->total_qty+$quantity->total_qty;?>"></td>
                                </tr>
                            </table>
                        </td>
                        <td width="10%">
                            <input type="hidden" name="publication_id" value="<?php echo $quantity->publication_id;?>">
                            <a id="changeCurrentState_<?php echo $quantity->publication_id;?>" href="javascript:void(0);" onclick="changeCurrentState('<?php echo $quantity->publication_id;?>')">Edit</a>
                            <div id="btn_for_publication_<?php echo $quantity->publication_id;?>" style="display:none;">
                                <input type="button" value="Update" id="btn_update_quantity_<?php echo $quantity->publication_id;?>" name="btn_update_quantity_<?php echo $quantity->publication_id;?>">
                            </div>
                        </td>
                                </tr>
                            </table></form>
                        </td>
                    </tr>
       
              <?php $cnt++;endforeach; ?>
              <?php else: ?>
                <tr>
                        <td colspan="3">
                        <div class="error_message">
                            No Quantity have been added.
                        </div>
                    </td>
                </tr>
          <?php endif; ?>
        </tbody>
    </table>
</div>

<div id="pagination">
	<?php echo $this->pagination->create_links(); ?>
</div>
<script>
   $('.calc_quantity').keyup(function() {
      var MyId = $($(this)).attr('id');
      var MyIdInfo = MyId.split('_');
      //var front = MyIdInfo[0]+"_"+MyIdInfo[1]; //unused
      var type = MyIdInfo[2]; //distribution, box, store
      var en_np =MyIdInfo[3]; //eng, nep
      var public_id =MyIdInfo[4]; //publication id
      var distribution_en = $('#total_qty_distribution_en_'+public_id).val();
      var box_en = $('#total_qty_box_en_'+public_id).val();
      var store_en = $('#total_qty_store_en_'+public_id).val();
      var distribution_np = $('#total_qty_distribution_np_'+public_id).val();
      var box_np = $('#total_qty_box_np_'+public_id).val();
      var store_np = $('#total_qty_store_np_'+public_id).val();
      
      if(isNaN(distribution_en) || distribution_en==""){
          distribution_en = 0;
          $('#total_qty_distribution_en_'+public_id).val(distribution_en);
      }
      if(isNaN(box_en) || box_en==""){
          box_en = 0;
          $('#total_qty_box_en_'+public_id).val(box_en);
      }
      if(isNaN(store_en) || store_en==""){
          store_en = 0;
          $('#total_qty_store_en_'+public_id).val(store_en);
      }
      if(isNaN(distribution_np) || distribution_np==""){
          distribution_np = 0;
          $('#total_qty_distribution_np_'+public_id).val(distribution_np);
      }
      if(isNaN(box_np) || box_np==""){
          box_np = 0;
          $('#total_qty_box_np_'+public_id).val(box_np);
      }
      if(isNaN(store_np) || store_np==""){
          store_np = 0;
          $('#total_qty_store_np_'+public_id).val(store_np);
      }

      var total_en = parseInt(distribution_en,10)+parseInt(box_en,10)+parseInt(store_en,10);
      //console.log(parseInt(distribution_en,10));
      var total_np = parseInt(distribution_np,10)+parseInt(box_np,10)+parseInt(store_np,10);
      var grand_total = parseInt(total_en,10)+parseInt(total_np,10);
      var distribution_total = parseInt(distribution_en,10)+parseInt(distribution_np,10);
      var box_total = parseInt(box_en,10)+parseInt(box_np,10);
      var store_total = parseInt(store_en,10)+parseInt(store_np,10);
      $('#total_qty_grand_en_'+public_id).val(total_en); //total_qty_grand_en_1
      $('#total_qty_grand_np_'+public_id).val(total_np); //total_qty_grand_np_1
      $('#total_qty_grand_total_'+public_id).val(grand_total); //total_qty_grand_total_1
      $('#total_qty_distribution_total_'+public_id).val(distribution_total); //total_qty_distribution_total_1
      $('#total_qty_box_total_'+public_id).val(box_total); //total_qty_box_total_1
      $('#total_qty_store_total_'+public_id).val(store_total); //total_qty_grand_total_1
   });
    function changeCurrentState(pub_id){
       var checkMe = $("#changeCurrentState_"+pub_id).hasClass('update_quantity');
        if(checkMe){
            var form_data = $("#frm_quantity_"+pub_id).serialize();
            var loading_image = "<img src='<?php echo base_url().'/assets/images/fb_loading.gif'?>'>";
            $("#changeCurrentState_"+pub_id).html(loading_image);
            $.ajax({
               type: "POST",
               url: "<?php echo site_url('quantity/ajax_update_quantity');?>",
               data: form_data,
               success: function(msg){
                   $("#show_msg").html(msg).fadeIn(300).fadeOut(300).fadeIn(300).fadeOut(300).fadeIn(300).fadeOut(300);
                  //updateFields(pub_id);
                  $("#changeCurrentState_"+pub_id).removeClass('update_quantity');
                  $("#changeCurrentState_"+pub_id).html('Edit');
                  changePublicationQuantityState('readonly', pub_id);
               }
             });
           //ajax update of sth;
           
        }else{
            $("#changeCurrentState_"+pub_id).addClass('update_quantity');
            $("#changeCurrentState_"+pub_id).html($("#btn_for_publication_"+pub_id).html());
            changePublicationQuantityState('edit', pub_id);
        }
    }
    function changePublicationQuantityState(type, pub_id){
        if(type=="edit"){
            $("#textbox_readonly-"+pub_id+" input").removeClass('textbox_readonly').removeAttr('readonly');
            
        }else if(type=="readonly"){
            $("#textbox_readonly-"+pub_id+" input").addClass('textbox_readonly').attr('readonly','readonly');
        }else{
            return false;
        }
        $("#textbox_readonly-"+pub_id+" input.permanent_readonly").addClass('textbox_readonly').attr('readonly','readonly');
    }
  function suppressNonNumericInput(event){
    if( !(event.keyCode == 8                                // backspace
        || event.keyCode == 46                              // delete
        || (event.keyCode >= 35 && event.keyCode <= 40)     // arrow keys/home/end
        || (event.keyCode >= 48 && event.keyCode <= 57)     // numbers on keyboard
        || (event.keyCode >= 96 && event.keyCode <= 105))   // number on keypad
        ) {
            event.preventDefault();     // Prevent character input
            
    }
}
</script>